package com.project.lx.gettxtfilename.RecycleView.divider;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.orhanobut.logger.Logger;
import com.project.lx.gettxtfilename.R;

/**
 * @author: X_Meteor
 * @description: 类描述
 * @version: V_1.0.0
 * @date: 2017/5/27 9:49
 * @company:
 * @email: lx802315@163.com
 */
public class TimelienItemDecoration extends RecyclerView.ItemDecoration {

    private Paint mPaint;
    private Paint mTextPaint;
    //ItemView左边的间距
    private float mOffsetLeft;
    //ItemView右边的间距
    private float mOffsetTop;
    //时间轴结点的半径
    private float mNodeRadius;

    public TimelienItemDecoration(Context context) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(40);
        mTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        mOffsetLeft = context.getResources().getDimension(R.dimen.timeline_item_offset_left);
        mNodeRadius = context.getResources().getDimension(R.dimen.timeline_item_node_radius);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

//        //第一个ItemView不需要在上面绘制分割线
        if (parent.getChildAdapterPosition(view) != 0) {
            //这里直接硬编码为1px
            outRect.top = 1;
            mOffsetTop = 1;
        }

        outRect.left = (int) mOffsetLeft;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);

        int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);

            int index = parent.getChildAdapterPosition(view);

            float dividerTop = view.getTop() - mOffsetTop;
            //第一个ItemView 没有向上方向的间隔
            if (index == 0) {
                dividerTop = view.getTop();
            }


            float dividerLeft = parent.getPaddingLeft();
            float dividerBottom = view.getBottom();
            float dividerRight = parent.getWidth() - parent.getPaddingRight();

            Logger.t("zuobiao").i("top" + dividerTop + "left" + dividerLeft + "bottom" + dividerBottom + "right" + dividerRight);
            c.drawLine(dividerLeft + mOffsetLeft, dividerTop, dividerRight, dividerTop, mPaint);

            float centerX = dividerLeft + mOffsetLeft / 2;
            float centerY = dividerTop + (dividerBottom - dividerTop) / 2;

            float upLineTopX = centerX;
            float upLineTopY = dividerTop;
            float upLineBottomX = centerX;
            float upLineBottomY = centerY - mNodeRadius;

            //绘制上半部轴线
            c.drawLine(upLineTopX, upLineTopY, upLineBottomX, upLineBottomY, mPaint);

            String s = index + "";
            float x = centerX - computeTextWidth(s) / 2;
            float y = centerY + computeTextHeight() / 3;

            //绘制时间轴结点
            mPaint.setStyle(Paint.Style.STROKE);
            c.drawCircle(centerX, centerY, mNodeRadius, mPaint);
            c.drawText(s, x, y, mTextPaint);
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

            float downLineTopX = centerX;
            float downLineTopY = centerY + mNodeRadius;
            float downLineBottomX = centerX;
            float downLineBottomY = dividerBottom;

            //绘制上半部轴线
            c.drawLine(downLineTopX, downLineTopY, downLineBottomX, downLineBottomY, mPaint);
        }
    }

    /**
     * 计算文字高度
     *
     * @return
     */
    private int computeTextHeight() {
        //使用FontMetrics对象，计算文字的坐标。
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        float textHeight = fontMetrics.descent - fontMetrics.ascent;
        return (int) textHeight;
    }

    /**
     * 计算文字宽度
     *
     * @return
     */
    private float computeTextWidth(String text) {
        return mTextPaint.measureText(text);
    }
}

